<%--
  Created by IntelliJ IDEA.
  User: gaosubo3000
  Date: 2021/4/12
  Time: 13:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>only Office</title>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8 ? MYOA_CHARSET : htmlspecialchars($HTML_PAGE_CHARSET))?>" />
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="/js/base/base.js"></script>
    <%--    <script src="/ui/lib/onlyoffice/api.js"></script>--%>

</head>
<body>
<div class="box">
    <div id="placeholder">正在加载中...</div>
</div>
</body>
<%--<script src="http://119.3.215.83:9001/web-apps/apps/api/documents/api.js" ></script>--%>
<script>

    <%--    附件类型--%>
    var getDocumentType = function (ext) {
        if (".doc.docx.docm.dot.dotx.dotm.odt.fodt.ott.rtf.txt.html.htm.mht.pdf.djvu.fb2.epub.xps".indexOf(ext) != -1) return "text";
        if (".xls.xlsx.xlsm.xlt.xltx.xltm.ods.fods.ots.csv".indexOf(ext) != -1) return "spreadsheet";
        if (".pps.ppsx.ppsm.ppt.pptx.pptm.pot.potx.potm.odp.fodp.otp".indexOf(ext) != -1) return "presentation";
        return null;
    };
    //正则
    function getQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    function myEncodeURI(str) {
        return encodeURI(str).replace(/\+/g, "%2b").replace(/\@/g,"%40").replace(/\#/g,"%23").replace(/\&/g,"%26").replace(/\//g,"%2F").replace(/\?/g,"%3F").replace(/\￥/g,"%ef%bf%a5").replace(/\$/g,"%24").replace(/\！/g,"%ef%bc%81").replace(/\（/g,"%ef%bc%88").replace(/\）/g,"%ef%bc%89").replace(/\…/g,"%e2%80%a6%e2%80%a6");
    }
    <%--    查找对应附件--%>
    var AID =  $.GetRequest().AID;
    var MODULE = $.GetRequest().MODULE;



    var attUrl = '';
    var edit = $.GetRequest().edit;
    if(edit == 'false'){
        edit = false
    }else{
        edit = true
    }
    var data = {}
    //网络硬盘
    var netdisk = $.GetRequest().netdisk;
    // var _w_pathNetdisk = myEncodeURI(getQueryString('_w_pathNetdisk')).substring(0,myEncodeURI(getQueryString('_w_pathNetdisk')).length-3)
    var _w_pathNetdisk = $.GetRequest()._w_pathNetdisk;
    var _w_module = 'netdisk'
    var diskId = $.GetRequest()._w_fileid

    var http = ''
    var url = '';
    var title = '';
    var type = '';
    var documentType = '';
    var userInfo = '';
    //预览编辑
    // var edit = true
    if(AID != ''&&MODULE != ''){
        //同步请求
        $.ajaxSettings.async = false;
        var href = '/web-apps/apps/api/documents/api.js';
        // 获取配置信息
        $.get('/sysTasks/getSysParaList',{
            paraName:'ONLY_OFFICE_ADDRESS,OUTSIDE_ADDRESS'
        },function(json){
            if(json.flag){
                var objs = json.obj;
                for (var i = 0; i < objs.length; i++) {
                    var data = objs[i];
                    // 获取only office服务器地址 并引入相对应的js
                    if(data.paraName=='ONLY_OFFICE_ADDRESS'){
                        var head= document.getElementsByTagName('head')[0];
                        var script= document.createElement('script');
                        href = data.paraValue+href;
                        script.type= 'text/javascript';
                        script.src= href;
                        head.appendChild(script);
                    }

                    if(data.paraName=='OUTSIDE_ADDRESS'){
                        http = data.paraValue;
                        // http = 'http://yangpu.yanshi.xtdoa.cn/';
                    }
                }
            }
        })

        // 获取附件信息
        $.get('/attachment/findByAid',{
            aid:AID,
            module:MODULE
        },function(json){
            if(json.flag){
                attUrl = '&'+json.object.attUrl;
                title = json.object.attachName
                type = ('?'+title).substring(('?'+title).lastIndexOf(".")+1,('?'+title).length)
                documentType = getDocumentType(type);
            }
        })
        var onlyOfficeDownload = "/onlyOfficeDownload?code="
        //判断网络硬盘
        if(netdisk != undefined &&netdisk == 1){
            attUrl = '&path='+_w_pathNetdisk+'&diskId='+ diskId;
            title = decodeURI(_w_pathNetdisk).split('\\')[decodeURI(_w_pathNetdisk).split('\\').length-1]
            type = ('?'+title).substring(('?'+title).lastIndexOf(".")+1,('?'+title).length)
            documentType = getDocumentType(type);
            onlyOfficeDownload = '/netdisk/onlyOfficeDownload?code='
        }
        // 获取下载密钥
        $.ajax({
            url:'/onlyOfficeCode',
            dataType: 'json',
            type: 'post',
            success:function(res){
                if(res.flag){
                    code = res.obj;
                    userInfo = res.data;
                    url = http+onlyOfficeDownload+code+attUrl+'&type=preview';
                    setTimeout (function(){
                        initEditor();
                    },1000);

                }
            }
        })
    }


    // 初始化编辑器
    function initEditor(){
        var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= '/js/office/onlyoffice.js?20210517.6';
        head.appendChild(script);
    }




</script>
</html>
